<!DOCTYPE html>
<html xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      xmlns:th="http://www.thymeleaf.org">
<head th:include="/include/commonHeader::commonHeader('人员冲突编辑')"></head>
<body>
<div class="wrapper wrapper-content">
    <div class="col-sm-12">
        <form class="form-horizontal m-t" id="exampleForm">
            <input id="id" name="id" th:value="${crewConflict.id}" type="hidden">
            <div class="form-group">
                <div class="col-xs-3 form-label-v">
                    <label><font style="color:red;">*</font>人员A：</label>
                </div>
                <div class="col-xs-8">
                    <select id="pCode1" name="pCode1">
                    </select>
                </div>
            </div>
            <div class="form-group">
                <div class="col-xs-3 form-label-v">
                    <label><font style="color:red;">*</font>人员B：</label>
                </div>
                <div class="col-xs-8">
                    <select id="pCode2" name="pCode2">
                    </select>
                </div>
            </div>
            <div class="form-group">
                <div class="col-xs-3 form-label-v">
                    <label>备注：</label>
                </div>
                <div class="col-xs-8">
                    <textarea id="remark" name="remark" class="form-control" th:inline="text" ></textarea>
                </div>
            </div>
            <div class="form-group">
                <div class="text-center">
                    <button id="btn_save" onclick="save()" type="button" class="btn btn-primary">提交</button>
                </div>
            </div>
        </form>
    </div>
</div>

<script src="/plugins/bootstrapValidator/js/bootstrapValidator.min.js"></script>
<script src="/plugins/bootstrapValidator/js/language/zh_CN.js"></script>
<!-- 自定义js -->
<script th:inline="javascript">
    var prefix = "/crewConflict";
    var isReadonly = "[[${isReadonly}]]";//页面是否只能查看
    $(function () {
        //初始化下列框
        //select2InitialForCNamePCode("#pCode1",'人员姓名/拼音',null,[[${crewConflict.pCode1}]]);  //不分人员类型
        //select2InitialForCNamePCode("#pCode2",'人员姓名/拼音',null,[[${crewConflict.pCode2}]]);
        select2CommonAsync("#pCode1","/select2/getStaffPCode.do","人员姓名/首字母",[[${crewConflict.pCode1}]],null);
        select2CommonAsync("#pCode2","/select2/getStaffPCode.do","人员姓名/首字母",[[${crewConflict.pCode2}]],null);

        $('#pCode1').on('change',function(){
            $('#exampleForm').data('bootstrapValidator')
                .updateStatus('pCode2', 'NOT_VALIDATED', null)
                .validateField('pCode2');
        }); // Notify only Select2 of changes
        $('#pCode2').on('change',function(){
            $('#exampleForm').data('bootstrapValidator')
                .updateStatus('pCode1', 'NOT_VALIDATED', null)
                .validateField('pCode1');
        }); // Notify only Select2 of changes
        if (isReadonly === "1") {
            readonlyInput();
        } else {
            initValidator();
        }

        if([[${crewConflict.remark}]] != null){
            $("#remark").val([[${crewConflict.remark}]]);
        }
    });

    //禁用所有输入框
    function readonlyInput() {
        $("#exampleForm").find("input,textarea,checkbox,radio").attr("readonly", true);
        $("#exampleForm").find("select").prop("disabled", true);
        $("#btn_save").hide();//隐藏操作按钮
    }

    //保存
    function save() {
        $("#exampleForm").data('bootstrapValidator').validate();//触发一次所有的验证
        if (!$("#exampleForm").data('bootstrapValidator').isValid()) {//判断验证是否已经通过
            return;
        }

        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/update.do",
            data: $('#exampleForm').serialize(),
            error : function(request) {
                toastr.error("Connection error");
            },
            success : function(data) {
                if (data.code == 0) {
                    parent.toastr.success("操作成功");
                    parent.reload();
                    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                    parent.layer.close(index);
                } else {
                    toastr.warning(data.msg)
                }
            }
        });
    }

    //初始化验证规则
    function initValidator() {
        $('#exampleForm').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                pCode1: {
                    message: '人员A不能为空',
                    validators: {
                        notEmpty: {
                            message: '此项不能为空'
                        },
                        different: {
                            field: 'pCode2',
                            message: '不能同人员B相同'
                        }
                    }
                },
                pCode2: {
                    message: '人员B不能为空',
                    validators: {
                        notEmpty: {
                            message: '此项不能为空'
                        },
                        different: {
                            field: 'pCode1',
                            message: '不能同人员A相同'
                        }
                    }
                }
            }
        });
    }
</script>
</body>
</html>
